package com.mf.morefun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mf.morefun.R;
import com.mf.morefun.bean.DoHomeWorkListBean;
import com.mf.morefun.listener.HomeWorkCountListener;
import com.zhcx.zhcxlibrary.utils.StringUtils;

import java.util.List;
import java.util.Map;

public class HomeWorkCountAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private Map<String, List<DoHomeWorkListBean>> mHomeWorkMap;
    private List<DoHomeWorkListBean> mDoHomeWorkList;
    private HomeWorkCountListener listener;

    public HomeWorkCountListener getListener() {
        return listener;
    }

    public void setListener(HomeWorkCountListener listener) {
        this.listener = listener;
    }

    public HomeWorkCountAdapter(Context mContext, Map<String, List<DoHomeWorkListBean>> mHomeWorkMap, List<DoHomeWorkListBean> mDoHomeWorkList) {
        this.mContext = mContext;
        this.mHomeWorkMap = mHomeWorkMap;
        this.mDoHomeWorkList = mDoHomeWorkList;
    }

    @Override
    public int getGroupCount() {
        return mDoHomeWorkList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View groupView = null;
        if (null == convertView){
            groupView = LayoutInflater.from(mContext).inflate(R.layout.layout_homeworkcountitem, null);
        }else {
            groupView = convertView;
        }
        if (null != mDoHomeWorkList && mDoHomeWorkList.size() > 0){
            TextView tv_howeworkname = groupView.findViewById(R.id.tv_howeworkname);
            TextView tv_name = groupView.findViewById(R.id.tv_name);
            TextView tv_time = groupView.findViewById(R.id.tv_time);
            TextView tv_do = groupView.findViewById(R.id.tv_do);
            TextView tv_type = groupView.findViewById(R.id.tv_type);
            LinearLayout ll_bg = groupView.findViewById(R.id.ll_bg);
            ll_bg.setBackgroundResource(R.color.white);
            ll_bg.setBackgroundResource(R.color.white);
            //计算背景
            caulateBg(ll_bg, groupPosition);
            tv_howeworkname.setVisibility(View.VISIBLE);
            tv_name.setVisibility(View.INVISIBLE);
            tv_time.setVisibility(View.INVISIBLE);
            tv_type.setVisibility(View.INVISIBLE);
            tv_do.setVisibility(View.INVISIBLE);
            if (null != mDoHomeWorkList.get(groupPosition).getMfTpLRR()){
                tv_howeworkname.setText(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName());
            }else {
                tv_howeworkname.setText("未知");
            }
        }
        return groupView;
    }

    private void caulateBg(LinearLayout ll_bg, int groupPosition) {
        int position = 0;
        if (null != mDoHomeWorkList && mDoHomeWorkList.size() > 0){
            if (groupPosition == 0){
                ll_bg.setBackgroundResource(R.color.nomalbg_color);
            }else {
                if (null != mDoHomeWorkList.get((groupPosition - 1)).getMfTpRecordPageDataList() && mDoHomeWorkList.get((groupPosition - 1)).getMfTpRecordPageDataList().size() > 0){
                    position = (groupPosition * (mDoHomeWorkList.get(groupPosition).getMfTpRecordPageDataList().size())) + groupPosition;
                }else {
                    position = groupPosition;
                }
                if ((position % 2) == 0){
                    ll_bg.setBackgroundResource(R.color.nomalbg_color);
                }else {
                    ll_bg.setBackgroundResource(R.color.white);
                }
            }
        }
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View childView = null;
        if (null == convertView){
            childView = LayoutInflater.from(mContext).inflate(R.layout.layout_homeworkcountitem, null);
        }else {
            childView = convertView;
        }
        try {
            if (null != mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()) && mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).size() > 0){
                TextView tv_howeworkname = childView.findViewById(R.id.tv_howeworkname);
                TextView tv_name = childView.findViewById(R.id.tv_name);
                TextView tv_time = childView.findViewById(R.id.tv_time);
                TextView tv_do = childView.findViewById(R.id.tv_do);
                TextView tv_type = childView.findViewById(R.id.tv_type);
                LinearLayout ll_bg = childView.findViewById(R.id.ll_bg);
                ll_bg.setBackgroundResource(R.color.white);
                caulateBg(ll_bg,groupPosition, childPosition);
                tv_howeworkname.setVisibility(View.INVISIBLE);
                tv_name.setVisibility(View.VISIBLE);
                tv_time.setVisibility(View.VISIBLE);
                tv_do.setVisibility(View.VISIBLE);
                tv_name.setText(mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getUserName());
                if (null != mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getMfTpLRR()){
                    tv_time.setVisibility(View.VISIBLE);
                    tv_do.setVisibility(View.VISIBLE);
                    if (null != mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getMfTpRecord()){
                        tv_time.setText(mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getMfTpRecord().getCreateDate());
                    }else {
                        tv_time.setText("");
                        tv_type.setText("未完成");
                    }
                    if (StringUtils.isEmpty(mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getMfTpComment())){
                        tv_do.setBackgroundResource(R.drawable.gray_shapes);
                        tv_type.setText("未点评");
                    }else {
                        tv_do.setBackgroundResource(R.drawable.login_bg);
                        tv_type.setText("已点评");
                    }
                    tv_do.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (!StringUtils.isEmpty(mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getMfTpLRR().getType())){
                                if (mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getMfTpLRR().getType().equals("3")){
                                    //录音又分未完成和已完成
                                    if (StringUtils.isEmpty(mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getMfTpRecordFlag())) {
                                        listener.callBack(groupPosition, childPosition, 3);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                    } else {
                                        if (mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getMfTpRecordFlag().equals("0")) {
                                            listener.callBack(groupPosition, childPosition, 3);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                        } else {
                                            listener.callBack(groupPosition, childPosition, 4);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                        }
                                    }
                                }else if(mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getMfTpLRR().getType().equals("4")){
                                    listener.callBack(groupPosition, childPosition, 2);//直接去视频
                                }else {
                                    if (StringUtils.isEmpty(mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getMfTpRecordFlag())) {
                                        listener.callBack(groupPosition, childPosition, 0);//创作 0  磨耳朵1  阅读2  录音3
                                    } else {
                                        if (mHomeWorkMap.get(mDoHomeWorkList.get(groupPosition).getMfTpLRR().getName()).get(childPosition).getMfTpRecordFlag().equals("0")) {
                                            listener.callBack(groupPosition, childPosition, 0);//创作 0  磨耳朵1  阅读2  录音3
                                        } else {
                                            listener.callBack(groupPosition, childPosition, 1);//创作 0  磨耳朵1  阅读2  录音3
                                        }
                                    }
                                }
                            }
                        }
                    });
                }else {
                    tv_time.setVisibility(View.INVISIBLE);
                    tv_do.setVisibility(View.INVISIBLE);
                    tv_type.setText("未完成");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return childView;
    }

    private void caulateBg(LinearLayout ll_bg, int groupPosition, int childPosition) {
        if (null != mDoHomeWorkList && mDoHomeWorkList.size() > 0){
            if (null != mDoHomeWorkList.get(groupPosition).getMfTpRecordPageDataList() && mDoHomeWorkList.get(groupPosition).getMfTpRecordPageDataList().size() > 0){
                if ((mDoHomeWorkList.get(groupPosition).getMfTpRecordPageDataList().size() % 2) == 0){
                    if ((groupPosition % 2) == 0){
                        if ((childPosition % 2) == 0){
                            ll_bg.setBackgroundResource(R.color.white);
                        }else {
                            ll_bg.setBackgroundResource(R.color.nomalbg_color);
                        }
                    }else {
                        if ((childPosition % 2) == 0){
                            ll_bg.setBackgroundResource(R.color.nomalbg_color);
                        }else {
                            ll_bg.setBackgroundResource(R.color.white);
                        }
                    }
                }else {
                    if ((childPosition % 2) == 0){
                        ll_bg.setBackgroundResource(R.color.white);
                    }else {
                        ll_bg.setBackgroundResource(R.color.nomalbg_color);
                    }
                }
            }
        }
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
